package cn.px.rest.controller.purchase;

import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.R;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import cn.px.dao.dto.purchase.PurchasePlanListDTO;
import cn.px.dao.entity.purchase.PurchasePlanList;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.purchase.ExportPurchaseListVO;
import cn.px.dao.vo.export.purchase.ExportPurchasePlanListVO;
import cn.px.dao.vo.query.purchase.PurchasePlanListVO;
import cn.px.rest.log.annotation.SysLog;
import cn.px.service.purchase.PurchasePlanListService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description 采购明细 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/purchase/purchase-plan-lists")
public class PurchasePlanListController {

    private final PurchasePlanListService purchasePlanListService;

    /**
     * 通过id查询单条记录
     *
     * @param id 主键
     * @return R
     */
    @GetMapping("/{id}")
    public R<PurchasePlanList> getById(@PathVariable("id") String id) {
        return new R<>(purchasePlanListService.getModelById(id));
    }

    /**
     * 新增记录
     *
     * @param purchasePlanListDTO 采购明细DTO
     * @return R
     */
    @SysLog("新增采购计划明细")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('purchase-purchase-plan-lists-add')")
    public R save(@Validated({AddGroup.class}) @RequestBody PurchasePlanListDTO purchasePlanListDTO) {
        return new R<>(purchasePlanListService.saveModelByDTO(purchasePlanListDTO));
    }

    /**
     * 修改记录
     *
     * @param purchasePlanListDTO 采购明细DTO
     * @return R
     */
    @SysLog("修改采购明细")
    @PutMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-plan-lists-edit')")
    public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody PurchasePlanListDTO purchasePlanListDTO) {
        purchasePlanListService.modModelByDTO(purchasePlanListDTO);
        return new R<>();
    }

    /**
     * 修改记录
     *
     * @param records 采购明细DTO
     * @return R
     */
    @SysLog("修改采购明细")
    @PutMapping("/plan/{planId}")
//    @PreAuthorize("@pms.hasPermission('purchase-purchase-plan-lists-edit')")
    public R updateByPlanId(@PathVariable("planId") String planId, @RequestBody List<PurchasePlanListDTO> records) {
        purchasePlanListService.handleListByPlan(planId, records);
        return new R<>();
    }

    /**
     * 通过id删除一条记录
     *
     * @param id 主键
     * @return R
     */
    @SysLog("删除采购明细")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-plan-lists-del')")
    public R removeById(@PathVariable String id) {
        purchasePlanListService.deleteByPrimaryKey(id);
        return new R<>();
    }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除采购明细")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-plan-lists-del')")
    public R removeIds(@RequestBody List<String> ids) {
        purchasePlanListService.batchDelete(ids);
        return new R<>();
    }

    /**
     * 获取分页
     *
     * @param pageNum             当前页码
     * @param pageSize            每页条数
     * @param purchasePlanListDTO 采购明细DTO
     * @return R
     */
    @GetMapping("/page")
    public R<AbstractPageResultBean<PurchasePlanListVO>> query(PurchasePlanListDTO purchasePlanListDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
        return new R<>(purchasePlanListService.selectPageList(purchasePlanListDTO, pageNum, pageSize));
    }

    /**
     * 获取列表
     *
     * @param purchasePlanListDTO 采购明细DTO
     * @return R
     */
    @GetMapping("")
    public R query(PurchasePlanListDTO purchasePlanListDTO) {
        return new R<>(purchasePlanListService.getModelListByDTO(purchasePlanListDTO));
    }

    /**
     * 采购明细导出
     *
     * @param purchasePlanListDTO 采购明细DTO
     * @return R
     */
    @GetMapping("/export")
    @SysLog("采购明细导出")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-plan-lists-export')")
    public void export(HttpServletResponse response, PurchasePlanListDTO purchasePlanListDTO) {
        List<PurchasePlanList> list = purchasePlanListService.getModelListByDTO(purchasePlanListDTO);
        ExcelUtil.exportResponseDict(response, ExportPurchasePlanListVO.class, BeanConverterUtil.copyListProperties(list, ExportPurchaseListVO.class), "采购计划明细");
    }

}
